<template>
  <div class="reportFormPage">
    <div class="report-dom">
      <el-row>
        <el-col :span="5" :offset="19">
          <div class="pageNumBox">
            <span>第</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
            <span>页，共</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
            <span>页</span>
          </div>
        </el-col>
      </el-row>
      <div class="topTwoBox">
        <div class="centerName">路面压实度试验检测记录表(钻芯法)</div>
        <div class="codeText">
          <tr>
            JGLP01003c
          </tr>
        </div>
      </div>
      <div class="threeTopBox">
        <div class="left-border">
          <div class="box-title">检测单位名称：</div>
          <div class>{{ form.jiancdwmc }}</div>
        </div>
        <div class="right-border">
          <span>记录编号：</span>
          <span class="placeholder">{{ form.jilbh }}</span>
        </div>
      </div>
      <div class="table-border">
        <!-- .........................1111111111111111111111111111111......................................................... -->
        <table height="220px" class="top-table">
          <tbody>
            <tr>
              <td height="30" align="center" width="15%">
                <span>工程名称</span>
              </td>
              <td colspan="3" class="text-center">
                <span>{{ form.gongcmc }}</span>
              </td>
            </tr>
            <tr>
              <td height="30" align="center" width="15%">
                <span>工程部位/用途</span>
              </td>
              <td colspan="3" class="text-center">
                <span>{{ form.gongcbwyt }}</span>
              </td>
            </tr>
            <tr>
              <td height="50" align="center">样品信息</td>
              <td class="specimen-border" colspan="3">
                <span>样品名称：</span>
                <span :class="{ placeholder: !form.yangpmc }">{{ form.yangpmc }}</span>
                <span>；样品编号：</span>
                <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>
                <span>；样品数量：</span>
                <span :class="{ placeholder: !form.yangpsl }">{{ form.yangpsl }}</span>
                <span>；样品状态：</span>
                <span :class="{ placeholder: !form.yangpzt }">{{ form.yangpzt }}</span>
                <span>；来样时间：</span>
                <span :class="{ placeholder: !form.yangpsj }">{{ form.yangpsj }}</span>
              </td>
            </tr>

            <tr>
              <td height="25" align="center" width="15%">
                <span>试验检测日期</span>
              </td>
               <!-- <td width="35%" class="text-center">{{ form.shiyjcrq }}</td> -->                                            
 <td width="35%" class="text-center">
      <el-input v-model="form.shiyjcrq" />
</td>
              <td align="center" width="15%">
                <span>试验条件</span>
              </td>
              <td v-if="isPdf" width="35%">
                温度：
                <span style="display: inline-block; width: 10%">{{ form.shiytjwd }}</span>
                ℃；湿度：
                <span style="display: inline-block; width: 10%">{{ form.shiytjsd }}</span
                >%RH
              </td>
              <td v-else width="35%">
                温度：
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjwd" style="width: 15%" />℃；湿度：
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjsd" style="width: 15%" />%RH
              </td>
            </tr>

            <tr>
              <td height="50" align="center" width="15%">
                <span>检测依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.jiancyj }}</span>
                <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark" @dblclick.native="showBsJiancyjView" readonly />
              </td>
              <td align="center" width="15%">
                <span>判定依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.pandyj }}</span>
                <el-input v-else type="textarea" v-model="form.pandyj" :rows="2" @dblclick.native="showBsJudgeBaseView" @focus="handleFocus('pandyj')" class="mark" />
              </td>
            </tr>
            <tr class="bottom-border">
              <td height="50" align="center">
                <div>主要仪器设备</div>
                <div>名称及编号</div>
              </td>
              <td colspan="3">
                <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh" class="height100 width100 mark" @dblclick.native="showBsInstrumentView" readonly></el-input>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .................................22222222222222222222222222222222............................................... -->
        <table height="25px" class="middle-table">
          <tr height="25">
            <td width="15%">沥青混合料级配类型</td>
            <td width="35%">
              <el-input v-model="form.liqhhljplx" @blur="setLmysdBgDataTop()" @keydown.native="handleKeyDown($event, 0, 0)" :ref="'0_0'" />
            </td>
            <td width="15%">保证率(%)</td>
            <td width="35%">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.baozl" @blur="setLmysdBgDataTop()" @keydown.native="handleKeyDown($event, 0, 1)" :ref="'0_1'" />
            </td>
          </tr>
        </table>
        <table height="25px" class="middle-table">
          <tr height="25">
            <td width="15%">理论相对密度</td>
            <td width="94px">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.lilxdmd" @blur="lilxdmdChange()" @keydown.native="handleKeyDown($event, 1, 0)" :ref="'1_0'" />
            </td>
            <td width="100px">路面标准相对密度</td>
            <td width="100px">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.lumbzxdmd" @blur="lumbzxdmdChange()" @keydown.native="handleKeyDown($event, 1, 1)" :ref="'1_1'" />
            </td>
            <td>路面标准密度压实度设计值(%)</td>
            <td width="100px">
              <el-input :type="isPdf ? 'string' : 'number'" v-model="form.lumbzmdycdsjz" @blur="setLmysdBgDataTop()" @keydown.native="handleKeyDown($event, 1, 2)" :ref="'1_2'" />
            </td>
          </tr>
        </table>
        <table height="580px" class="middle-table">
          <tbody>
            <tr height="25">
              <td class="left-td1">试件编号</td>
              <td class="left-td2">取样位置</td>
              <td class="td1">试样厚度(mm)</td>
              <td class="td1">干燥试件空气中质量(g)</td>
              <td class="td1">试件水中质量(g)</td>
              <td class="td1">试件表干质量(g)</td>
              <td class="td1">试件毛体积相对密度</td>
              <td class="td1">试件毛体积密度(g/cm³)</td>
              <td class="td1">空隙率(%)</td>
              <td class="td1">标准密度压实度(%)</td>
              <td class="td1">理论密度压实度(%)</td>
            </tr>
            <tr height="25" v-for="(item, index) in form.detailList" :key="index">
              <td>
                <el-input type="text" v-model="item.shijbh" @blur="setLmysdBgData()" @keydown.native="handleKeyDown($event, index + 2, 0)" :ref="`${index + 2}_0`" />
              </td>
              <td>
                <el-input type="text" v-model="item.qvywz" @blur="setLmysdBgData()" @keydown.native="handleKeyDown($event, index + 2, 1)" :ref="`${index + 2}_1`" />
              </td>
              <td>
                <el-input :type="isPdf ? 'string' : 'number'" v-model="item.shijhd" @blur="setLmysdBgData()" @keydown.native="handleKeyDown($event, index + 2, 2)" :ref="`${index + 2}_2`" />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  v-model="item.ganzsjkqzzl"
                  @blur="calculateShijmtjxdmd(index)"
                  @keydown.native="handleKeyDown($event, index + 2, 3)"
                  :ref="`${index + 2}_3`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  v-model="item.shijszzl"
                  @blur="calculateShijmtjxdmd(index)"
                  @keydown.native="handleKeyDown($event, index + 2, 4)"
                  :ref="`${index + 2}_4`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  v-model="item.shijbgzl"
                  @blur="calculateShijmtjxdmd(index)"
                  @keydown.native="handleKeyDown($event, index + 2, 5)"
                  :ref="`${index + 2}_5`"
                />
              </td>
              <td>
                <el-input :type="isPdf ? 'string' : 'number'" v-model="item.shijmtjxdmd" disabled />
              </td>
              <td>
                <el-input :type="isPdf ? 'string' : 'number'" v-model="item.shijmtjmd" disabled />
              </td>
              <td>
                <el-input :type="isPdf ? 'string' : 'number'" v-model="item.kongxl" disabled />
              </td>
              <td>
                <el-input :type="isPdf ? 'string' : 'number'" v-model="item.biaozmdysd" disabled />
              </td>
              <td>
                <el-input :type="isPdf ? 'string' : 'number'" v-model="item.lilmdysd" disabled />
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .....................................................33333333333333333................................................... -->
        <table height="30px" class="bottom-table">
          <tr>
            <td>
              <span v-if="isPdf" class="textarea-span">{{ form.fujsm }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.fujsm" @focus="handleFocus('fujsm')" />
            </td>
          </tr>
        </table>
      </div>
      <!-- .................................44444444444............................................. -->
      <table height="22px" class="transparent">
        <tbody>
          <tr>
            <td width="10%" align="right">检测：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">记录：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">复核：</td>
            <td align="left" width="15%"></td>
            <td width="10%">日期：</td>
            <td align="left" width="15%">
              <div style="display: inline-block; margin-left: 20px">年</div>
              <div style="display: inline-block; margin-left: 20px">月</div>
              <div style="display: inline-block ;margin-left: 20px">日</div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
    <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
    <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
  </div>
</template>

<script>
import { listLmysdjlbBsInfo, getLmysdjlbBsInfo, delLmysdjlbBsInfo, addLmysdjlbBsInfo, updateLmysdjlbBsInfo } from "@/api/report_ljlm/lmysdjlbBsInfo";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BaQjDetailView from "@/views/sycs/baQjDetail/BaQjDetailView.vue";
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";

import decimal from "@/utils/big-decimal";
import { handleFocus, emptyConvert, handleKeyDown } from "@/views/reportForms/utils";
import Bus from "@/utils/bus";
export default {
  name: "lmysdjlb",
  components: {
    BsInstrumentView,
    BaQjDetailView,
    BsJiancyjView,
    BsJudgeBaseView
  },
  props: {
    excelId: {
      type: [String, Number],
      default: () => ""
    },
    syType: {
      type: [String, Number],
      default: () => ""
    },
    page: {
      type: Number,
      default: () => 0
    },
    isPdf: {
      type: Boolean,
      default: () => false
    },
    printEmpty: {
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
      params: {},
      query: {},
      // 表单参数
      form: {
        detailList: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
      }
    };
  },
  watch: {
    isPdf(newVal, oldVal) {
      this.form = { ...emptyConvert(this.form, newVal) };
    }
  },
  created() {
    this.getInfo();
  },
  mounted() {
    // 获取顶部数据
    Bus.$on("setLmysdBgDataTop", (data) => {
      for (const key in data) {
        if (Object.prototype.hasOwnProperty.call(data, key)) {
          this.form[key] = data[key];
        }
      }
      this.form.detailList.forEach((item, index) => {
        this.calculateBiaozmdysd(index);
      });
      this.setLmysdBgData();
    });
  },

  beforeDestroy() {
    // 停止监听事件
    Bus.$off("setLmysdBgDataTop");
  },
  methods: {
    // 获取详情
    getInfo() {
      getLmysdjlbBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
        this.form = response.data;
        if (!this.form.fujsm) {
          this.form.fujsm = "附加声明：";
        }
      });
    },
    // 显示检测依据列表
    showBsJiancyjView() {
      this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
    },
    // 检测依据选择
    selectJiancyj(info) {
      this.form.jiancyj = info.label;
      this.form.jiancyjIds = info.ids;
      Bus.$emit("setLmysdBgJiancyj", { jiancyj: this.form.jiancyj, jiancyjIds: this.form.jiancyjIds });
    },
    // 显示判定依据列表
    showBsJudgeBaseView() {
      this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
    },
    // 判断依据选择
    selectJudge(info) {
       this.form.pandyjIds = info.ids;
      this.form.pandyj = info.label;
    },
    // 显示仪器列表
    showBsInstrumentView() {
      this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
    },
    // 仪器选择
    selectInstrument(info) {
      this.form.zhuyyqsbmcjbh = info.label;
      this.form.shebIds = info.ids;
      Bus.$emit("setLmysdBgZhuyyqsbmcjbh", { zhuyyqsbmcjbh: this.form.zhuyyqsbmcjbh, shebIds: this.form.shebIds });
    },
    // 计算试件毛体积相对密度
    calculateShijmtjxdmd(index) {
      // E=A/(C-B)
      const A = this.form.detailList[index].ganzsjkqzzl;
      const B = this.form.detailList[index].shijszzl;
      const C = this.form.detailList[index].shijbgzl;
      let E = null;
      if (A && B && C) {
        E = decimal.divide(A, decimal.subtract(C, B, 8), 3);
      }
      this.form.detailList[index].shijmtjxdmd = E;
      this.calculateShijmtjmd(index);
      this.calculateKongxl(index);
      this.calculateBiaozmdysd(index);
      this.calculateLilmdysd(index);
    },
    // 计算试件毛体积密度 shijmtjmd
    calculateShijmtjmd(index) {
      // F=E*0.9971
      const E = this.form.detailList[index].shijmtjxdmd;
      let F = null;
      if (E) {
        F = decimal.multiply(E, 0.9971, 3);
      }
      this.form.detailList[index].shijmtjmd = F;
    },
    // 计算空隙率
    calculateKongxl(index) {
      // (1-E/J)*100
      const E = this.form.detailList[index].shijmtjxdmd;
      const J = this.form.lilxdmd; // 理论相对密度
      let sum = null;
      if (E && J) {
        sum = decimal.multiply(decimal.subtract(1, decimal.divide(E, J, 10), 10), 100, 1);
      }
      this.form.detailList[index].kongxl = sum;
    },
    // 计算标准密度压实度
    calculateBiaozmdysd(index) {
      // 100*E/K
      const E = this.form.detailList[index].shijmtjxdmd;
      const K = this.form.lumbzxdmd; // 路面标准相对密度
      let sum = null;
      if (E && K) {
        sum = decimal.multiply(decimal.divide(E, K, 10), 100, 1);
      }
      this.form.detailList[index].biaozmdysd = sum;
    },
    // 计算理论密度压实度
    calculateLilmdysd(index) {
      // 100*E/J
      const E = this.form.detailList[index].shijmtjxdmd;
      const J = this.form.lilxdmd; // 理论相对密度
      let sum = null;
      if (E && J) {
        sum = decimal.multiply(decimal.divide(E, J, 10), 100, 1);
      }
      this.form.detailList[index].lilmdysd = sum;
      this.setLmysdBgData();
    },
    // 理论相对密度改变
    lilxdmdChange() {
      this.form.detailList.forEach((item, index) => {
        this.calculateKongxl(index);
        this.calculateLilmdysd(index);
      });
      this.setLmysdBgDataTop();
      this.setLmysdBgData();
    },
    // 路面标准相对密度改变
    lumbzxdmdChange() {
      this.form.detailList.forEach((item, index) => {
        this.calculateBiaozmdysd(index);
      });
      this.setLmysdBgDataTop();
      this.setLmysdBgData();
    },
    // 给报告传值
    setLmysdBgDataTop() {
      const info = {
        liqhhljplx: this.form.liqhhljplx,
        baozl: this.form.baozl,
        lilxdmd: this.form.lilxdmd,
        lumbzxdmd: this.form.lumbzxdmd,
        lumbzmdycdsjz: this.form.lumbzmdycdsjz
      };
      Bus.$emit("setLmysdBgDataTop", info);
    },
    setLmysdBgData() {
      Bus.$emit("setLmysdBgData" + this.page, this.form.detailList);
    },
    submitForm() {
      return this.form;
    },
    handleFocus(event) {
      handleFocus(event, this.page, this.form[event]);
    },
    handleKeyDown(event, rowIndex, colIndex) {
      const maxArr = [1, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5];
      const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
      if (this.$refs[ref]?.length) {
        this.$refs[ref][0].focus();
      } else if (this.$refs[ref]) {
        this.$refs[ref].focus();
      }
    }
  }
};
</script>

<style scoped>
.left-td1 {
  width: 35px;
}

/* .left-td4 {
  width: calc((40% - 35px));
} */

.width10 {
  width: 45px;
}

.td1 {
  width: 50px;
}
</style>
